package com.ylz.aidlmanager;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.ylz.aidlmanager.card.AidlCard;
import com.ylz.aidlmanager.printer.AidlPrinter;

/**
 * 项目名称：com.ylz.aidlmanager
 * 描述：一个服务管理多个aidl
 * 作者：FangZhiXin
 * 日期：2019/5/24 10:42
 */
public class AidlService extends Service {

    private static final String TAG = "AidlService";


    @Override
    public IBinder onBind(Intent intent) {
        return new AidlManagerInterface();
    }

    public class AidlManagerInterface extends IAidlManager.Stub {

        public static final int DRIVE_CARD = 1;
        public static final int DRIVE_PRINTER = 2;
        public static final int DRIVE_KEYBOARD = 3;

        @Override
        public String getVersion() throws RemoteException {
            return MyApplication.getVersion();
        }

        @Override
        public IBinder getDrive(int type) throws RemoteException {
            Binder binder = null;
            switch (type) {
                case DRIVE_CARD:
                    binder = new CardAidlInterface();
                    break;
                case DRIVE_PRINTER:
                    binder = new PrinterAidlInterface();
                    break;
                case DRIVE_KEYBOARD:
                    break;
            }
            return binder;
        }
    }

    public class CardAidlInterface extends AidlCard.Stub {

        @Override
        public boolean open() throws RemoteException {
            Log.i(TAG, "CardAidlInterface open: ");
            return true;
        }

        @Override
        public boolean close() throws RemoteException {
            Log.i(TAG, "CardAidlInterface close: ");
            return true;
        }

        @Override
        public String readCard() throws RemoteException {
            Log.i(TAG, "CardAidlInterface readCard: ");
            return "123456789";
        }
    }

    public class PrinterAidlInterface extends AidlPrinter.Stub {

        @Override
        public boolean open() throws RemoteException {
            Log.i(TAG, "PrinterAidlInterface open: ");
            return true;
        }

        @Override
        public boolean close() throws RemoteException {
            Log.i(TAG, "PrinterAidlInterface open: ");
            return true;
        }

        @Override
        public String print() throws RemoteException {
            Log.i(TAG, "PrinterAidlInterface print: ");
            return "成功";
        }
    }
}
